const WS_ADDRESS = 'ws://172.16.106.63:8080/websocket/'
const userIndexd = 'user-1'
function useWebSocket(handleMessage, handleClose) {

  const ws = new WebSocket(WS_ADDRESS + userIndexd)
  const init = () => {
    bindEvent()
  }
  function bindEvent() {
    ws.addEventListener('open', handleOpen, false); // 成功回调
    ws.addEventListener('close', handleClose, false); // 关闭回调 
    ws.addEventListener('error', handleError, false); // 错误回调
    ws.addEventListener('message', handleMessage, false); // 接收信息
  }
  function handleOpen(e) {
    console.log('WebSocket open,开启', e)
  }
  // function handleClose(e) {
  //   console.log('WebSocket close,断开了连接',e)
  // }
  function handleError(e) {
    console.log('WebSocket error,连接发生了错误', e)
  }

  init()
  return ws
}

export default useWebSocket
